package fr.inserm.umr915.vcfannotator.annotators;

import fr.inserm.umr915.libjson.Node;
import fr.inserm.umr915.libjson.NodeType;
import fr.inserm.umr915.libjson.ObjectNode;



public class Field
	{
	private NodeType type=NodeType.STRING;
	private String name=null;
	private Integer column=null;
	private String value=null;
	
	public Field()
		{
		}		
	
	public NodeType getType() {
		return type;
		}
	
	public void setType(NodeType type) {
		this.type = type;
		}
	
	public String getName() {
		return name;
		}
	
	public void setName(String name) {
		this.name = name;
		}
	
	public Integer getColumn() {
		return column;
		}
	
	public void setColumn(Integer column) {
		this.column = column;
		}
	
	public String getValue() {
		return value;
		}
	
	public void setValue(String value) {
		this.value = value;
		}
	
	public boolean annotate(ObjectNode annotation,String tokens[])
		{
		Node val=null;
		String s=getValue();
		if(getColumn()!=null)
			{
			s=tokens[getColumn()];
			}
			
		switch(getType())
			{
			case STRING:
				{
				if(!s.isEmpty())
					{
					val=annotation.getNodeFactory().createStringNode(s);
					}
				break;
				}
			case INT:
				{
				if(!s.isEmpty())
					{
					val=annotation.getNodeFactory().createIntNode(s);
					}
				break;
				}
			case FLOAT:
				{
				if(!s.isEmpty())
					{
					val=annotation.getNodeFactory().createFloatNode(s);
					}
				break;
				}
			case BOOLEAN:
				{
				if(!s.isEmpty())
					{
					val=annotation.getNodeFactory().createBooleanNode(s);
					}
				break;
				}
			default:throw new IllegalArgumentException("cannot handle column type "+getType());
			}
		if(val==null) return false;
			
		annotation.put(getName(), val);
		return true;
		}
	
	@Override
	public String toString()
		{
		return "Field [column=" + column + ", name=" + name + ", type=" + type
				+ ", value=" + value + "]";
		}
	}
